package com.nbsaas.boot.customer.rest.resource;

import com.nbsaas.boot.customer.api.apis.MemberProfileApi;
import com.nbsaas.boot.entity.customer.MemberProfile;
import com.nbsaas.boot.customer.api.domain.request.MemberProfileRequest;
import com.nbsaas.boot.customer.api.domain.response.MemberProfileResponse;
import com.nbsaas.boot.customer.api.domain.simple.MemberProfileSimple;
import com.nbsaas.boot.customer.rest.convert.MemberProfileSimpleConvert;
import com.nbsaas.boot.customer.rest.convert.MemberProfileEntityConvert;
import com.nbsaas.boot.customer.rest.convert.MemberProfileResponseConvert;
import com.nbsaas.boot.customer.data.repository.MemberProfileRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.enums.StoreState;
import com.nbsaas.boot.rest.request.RequestId;
import java.util.Optional;
import com.nbsaas.boot.rest.response.ResponseObject;
/**
*   会员-业务接口实现
*/
@Transactional
@Service
public class MemberProfileResource extends BaseResource<MemberProfile,MemberProfileResponse, MemberProfileSimple, MemberProfileRequest>  implements MemberProfileApi {

    @Resource
    private MemberProfileRepository memberProfileRepository;

    @Override
    public JpaRepositoryImplementation<MemberProfile, Serializable> getJpaRepository() {
        return memberProfileRepository;
    }

    @Override
    public Function<MemberProfile, MemberProfileSimple> getConvertSimple() {
        return new MemberProfileSimpleConvert();
    }

    @Override
    public Function<MemberProfileRequest, MemberProfile> getConvertForm() {
        return new MemberProfileEntityConvert();
    }

    @Override
    public Function<MemberProfile, MemberProfileResponse> getConvertResponse() {
        return new MemberProfileResponseConvert();
    }




    @Override
    public ResponseObject<MemberProfileResponse> create(MemberProfileRequest request) {
        request.setStoreState(StoreState.normal);
        return super.create(request);
   }

    @Override
    public ResponseObject<?> delete(RequestId request) {

        ResponseObject<?> result = new ResponseObject<>();
        Optional<MemberProfile> optional = getJpaRepository().findById(request.getId());
        if (!optional.isPresent()) {
            result.setCode(501);
            result.setMsg("无效id");
            return result;
        }
        MemberProfile bean = optional.get();
        bean.setStoreState(StoreState.draft);
        return result;

    }
}


